﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using CMP.ERP.Admin.ServiceReference1;

namespace CMP.ERP.Admin.Models
{
    public class ClientController : Controller
    {
        //
        // GET: /Client/
        ServiceReference1.Service1Client client = new Service1Client();//实例化代理类
        /// <summary>
        /// 显示客户信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Client_Show()
        {
            ViewBag.CityID = new SelectList(client.ShowCity().ToList(), "CityID", "CityName");
            List<ServiceReference1.CustomersInfo> cu = new List<CustomersInfo>();
            cu = client.ShowCustomersInfo().ToList();//接受XML转化尾List
            return View(cu);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="cu"></param>
        /// <returns></returns>
        public ActionResult AddCustomersInfo(CustomersInfo cu)
        {
            if (client.AddCustomersInfo(cu) > 0)
            {
                return Content("<script>alert('添加成功'); location.href='/Client/Client_Show'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败'); location.href='/Client/Client_Show/'</script>");
            }
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DelCustomersInfo(int id)
        {
            if (client.DelCustomersInfo(id) > 0)
            {
                return Content("<script>alert('删除成功'); location.href='/Client/Client_Show'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败'); location.href='/Client/Client_Show/'</script>");
            }
        }
        /// <summary>
        /// 修改的视图
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdCustomersInfoShow(int id)
        {
            ViewBag.CityID = new SelectList(client.ShowCity().ToList(), "CityID", "CityName");
            return View(client.GetCustomersInfoByid(id));
        }
        /// <summary>
        /// 修改的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult UptCustomersInfo(CustomersInfo cu)
        {
            if (client.UptCustomersInfo(cu) > 0)
            {
                return Content("<script>alert('修改成功'); location.href='/Client/Client_Show'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败'); location.href='/Client/Client_Show/'</script>");
            }
        }

        /// <summary>
        /// 根据姓名搜索
        /// </summary>
        /// <param name="CustomersName"></param>
        /// <returns></returns>
        
        public ActionResult SearchCustomersInfo(string CustomersName)
        {
            ViewBag.CityID = new SelectList(client.ShowCity().ToList(), "CityID", "CityName");
            List<ServiceReference1.CustomersInfo> cu = client.GetCustomersInfoByCustomersName(CustomersName).ToList();
            
            return View("Client_Show",cu );
        }
       
    }
}
